﻿Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Imports System.Data.SqlClient
Imports System.Net
Imports System.Xml




Public Module DS1
    Private xlApp As Excel.Application
    Private xlWorkBook As Excel.Workbook
    Private xlWorkSheet As Excel.Worksheet
    Private dirExport As String
    Private dirLog As String
    Private dirInstall As String
    Private archXls As String
    Private num As Integer = 1
    Private conf As Boolean
    Private Timer As Integer
    Private dirFTP As String



    Sub inicializar()

        Try
            log("inicializar")

            Dim xmldoc As New XmlDocument()
            Dim a As String
            Dim b As Integer


            Try
                dirInstall = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Anew\AppQfinitiExport", "dirInstall", Nothing).ToString.Trim
            Catch ex As Exception
                dirInstall = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Wow6432Node\Anew\AppQfinitiExport", "dirInstall", Nothing).ToString.Trim
            End Try


            xmldoc.Load(dirInstall & "ConfigAppExport.xml")



            Timer = xmldoc.SelectSingleNode("/CONFIG/TIMER").InnerXml


            a = xmldoc.SelectSingleNode("/CONFIG/DIREXPORT").InnerXml
            b = a.Length

            If a = "" Then
                dirExport = dirInstall
            Else

                If InStr(b - 1, a, "\") = 0 Then
                    dirExport = a.Insert(b, "\")
                Else
                    dirExport = a
                End If

            End If

            a = xmldoc.SelectSingleNode("/CONFIG/DIRLOG").InnerXml
            b = a.Length

            If a = "" Then
                dirLog = dirInstall
            Else

                If InStr(b - 1, a, "\") = 0 Then
                    dirLog = a.Insert(b, "\")
                Else
                    dirLog = a
                End If

            End If


        Catch ex As Exception

            log(ex.Message)
            dirExport = "C:\ToExport\"
            dirLog = dirInstall & "Logs"

            Timer = 3600000 'en milisegundos

        End Try


    End Sub

    Sub Main()


        Console.WriteLine("Main")

        inicializar()

        archXls = dirInstall & "Data.xls"
        dirFTP = dirInstall & "FTP\"


        My.Computer.FileSystem.CopyFile(dirInstall & "Template.xls", archXls, True)

        Try
            conf = False

            For Each foundFile As String In My.Computer.FileSystem.GetFiles(dirExport, FileIO.SearchOption.SearchTopLevelOnly, "*.wav")
                If foundFile Like "*-*_*_*_*-*.wav" Then
                    num = num + 1
                    ConvertXLS(foundFile)
                    conf = True
                End If
            Next

            If conf = True Then
                My.Computer.FileSystem.MoveFile(archXls, dirFTP & "Data.xls", True)
            End If

        Catch ex As Exception
            log(ex.Message)
            Exit Sub
        End Try
    End Sub


    Sub ConvertXLS(ByVal archWAV As String)
        Dim p, p2 As Integer
        Dim trID As String
        Dim a As String = archWAV.Replace(dirExport, "")
        Dim b As String
        Dim Agent As String
        Dim Fecha As String
        Dim Tiempo As String
        Dim Duration As String
        Dim Duration2 As Double
        Dim DNIS As String
        Dim ANI As String
        Dim ConnID As String
        Dim CallDirection As String

        'audioFile = New Audio(archWAV, False)

        Try
            log("Leyendo Template: " & archXls)
            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Open(archXls)
            xlWorkSheet = xlWorkBook.Worksheets(1)


            p = InStr(a, "_")
            p2 = InStr(p + 1, a, "_")
            trID = a.Remove(p2)
            trID = trID.Replace("-", "")
            trID = trID.Replace("_", "")

            xlWorkSheet.Cells(num, 1).value = "'" & trID



            p = InStr(a, "-")
            p2 = InStr(a, "_")
            Fecha = a.Remove(p - 1)
            Tiempo = a.Remove(0, p).Remove(p2 - p - 1)
            b = a.Remove(0, p2)
            p2 = InStr(b, "_")
            DNIS = b.Remove(p2 - 1)
            p = InStr(p2 + 1, b, "_")
            b = b.Remove(0, p)
            p2 = InStr(b, "-")
            Agent = b.Remove(p2 - 1)
            'Duration2 = audioFile.Duration 'CAMBIOOOOOOOOO
            Duration = System.Convert.ToString(System.Convert.ToInt64(Duration2))
            Fecha = Fecha.Insert(4, "-").Insert(7, "-")
            Tiempo = Tiempo.Insert(2, ":").Insert(5, ":")
            ANI = ""
            ConnID = ""
            CallDirection = ""


            xlWorkSheet.Cells(num, 2).value = "XX, " & Agent
            xlWorkSheet.Cells(num, 3).value = "'" & Fecha
            xlWorkSheet.Cells(num, 4).value = "'" & Tiempo
            xlWorkSheet.Cells(num, 5).value = "'" & Duration
            xlWorkSheet.Cells(num, 6).value = DNIS
            xlWorkSheet.Cells(num, 7).value = "'7500"
            xlWorkSheet.Cells(num, 8).value = "OK"
            xlWorkSheet.Cells(num, 9).value = ""
            xlWorkSheet.Cells(num, 10).value = "Unknown"
            xlWorkSheet.Cells(num, 11).value = a




            xlWorkBook.Save()
            xlWorkBook.Close()
            xlApp.Quit()

            xlWorkSheet = Nothing
            xlWorkBook = Nothing
            xlApp = Nothing

            releaseObject(xlWorkSheet)
            releaseObject(xlWorkBook)
            releaseObject(xlApp)

            'audioFile = Nothing
            'releaseObject(audioFile)

            My.Computer.FileSystem.MoveFile(archWAV, dirFTP & a, True)




        Catch ex As Exception

            log(ex.Message)
            Exit Sub

        End Try

    End Sub



    Function FechayHora() As String
        FechayHora = Now.Date.ToString.Remove(InStr(Now.Date.ToString, " ")) & Now.TimeOfDay.ToString
    End Function

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub



    Sub log(ByVal mensaje As String)
        Try
            Dim oSW As New StreamWriter(dirInstall & "AppQfinitiExport.log", True)
            oSW.WriteLine(FechayHora & " " & mensaje)
            oSW.Flush()
            oSW.Close()
            releaseObject(oSW)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

   

End Module
